<template>
  <div class="container">
    <div class="app-container">
      <!-- 展示树形结构 -->
      <el-tree :expand-on-click-node="false" :data="depts" :props="defaultProps" :default-extend-all="true">
        <template v-slot="{ data }">
          <el-row style="width: 100%; height: 40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <el-col>{{ data.managerName }}</el-col>
                <el-col
                  ><el-dropdown @command="operateDept($event, data.id)">
                    <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                      <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                      <el-dropdown-item command="del">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown></el-col
                >
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 部门弹窗 -->
    <addDept
      ref="addDeptRef"
      :show-dialog.sync="showDialog"
      :current-node-id="currentNodeId"
      @updateDepartment="getDepartment"
    ></addDept>
  </div>
</template>
<script>
import { getDepartment, delDepartmentData } from '@/api/deparment.js'
import { tranListToTreeData } from '@/utils' // 引入转换数据的方法
import addDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      depts: [], // 部门数据
      defaultProps: {
        children: 'children',
        label: 'name'
      }, // 树形控件的配置项
      showDialog: false, // 控制弹窗显示隐藏
      currentNodeId: null // 记录当前操作的部门id
    }
  },
  created() {
    this.getDepartment() // 获取部门数据
  },
  methods: {
    async getDepartment() {
      const res = await getDepartment()
      this.depts = tranListToTreeData(res, 0)
    },
    operateDept(type, id) {
      if (type === 'add') {
        this.showDialog = true // 显示弹窗
        this.currentNodeId = id // 记录当前操作的部门id
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        // 延迟执行，等待弹窗渲染完成后再获取数据
        this.$nextTick(() => {
          this.$refs.addDeptRef.getDepartmentDetail(this.currentNodeId)
        })
      } else {
        // 删除部门时弹出确认框
        this.$confirm('您确认要删除该部门吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            this.currentNodeId = id
            // 调用删除接口，删除成功后重新获取部门数据
            await delDepartmentData(id)
            this.getDepartment()
            this.$message.success('删除部门成功')
            // this.$refs.addDeptRef.delDepartment(this.currentNodeId)
          })
          .catch(() => {
            this.$message.info('已取消删除')
          })
      }
    }
  }
}
</script>
<style lang="scss">
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}

</style>
